import numpy as np
import matplotlib.pyplot as mp
import interpol as it
import integral as ig
import load_foil as lf


def upper_curve(hmax , l , f):
    return [lambda x : (1-l)*f(x)+l*3*hmax]


def lower_curve(hmin , l , f):
    return [lambda x : (1-l)*f(x)+l*3*hmin]


def compute_pressure(f , density, integral):
    return 1/2*density*integral

#integral represents the result of the function which we have used to integer.


y2 = it.spline(it.ex,it.ey,it.ne,it.yp1,it.ypn)
#f = ig.g(y2,it.ne,it.ex,it.ey)
f = lambda x: np.sqrt(1+x**2)
# !!!!!!! Donne toujours 0 sur [0,1] !!!!!!
print ig.general_method(ig.right_rectangle_method,f,it.ne,0,0.99)

#------- Global variables ----- #

hmin = np.min(it.iy)
print hmin
hmax = np.max(it.ey)
print hmax


density = 0.2

def graph_pressure(f, a, b, N, integral):
    lamb = 0
    h = (b-a)/N
    upper = []
    lower = []
    pressure_up = []
    pressure_low = []
    
    for i in np.arange(0,N,1):
        lamb = lamb + h
        up = upper_curve(hmax, lamb, f)
        upper += [up]
        low = lower_curve(hmin, lamb, f)
        lower += [low]
        pressure_up += [compute_pressure(up, density, integral)]
        pressure_low += [compute_pressure(low, density, integral)]
        
    return (upper, lower, pressure_up, pressure_low)

resultat = graph_pressure(f, 0, 1, 10, ig.middle_dot_method(f,10,0,1))
print resultat

def plot_pressure(f,a,b,N):
    "Computes the pressure graph"
    mp.clf()
    (upper, lower, pressure_up, pressure_low) = graph_pressure(f, a, b, N, ig.middle_dot_method(f,N,a,b))
    mp.plot((pressure_up, pressure_low))
    mp.show()
    
plot_pressure(f, 0, 1, 10)

#On a les axes qui se sont bien parametres !
#Mais pas du tout de graphe des pressions. 
